export class FreeThree {
    static disposeObject3D(obj) {
        if (obj.isObject3D) {
            obj.traverse(FreeThree.disposeNode)
        }
        if (obj.isScene) {
            obj.clear()
            if (obj?.my?.destroy) {
                obj.my.destroy()
            }
        }
    }

    static disposeNode(node) {
        if (node.isMesh) {
            const { geometry, material } = node
            geometry?.dispose()
            if (Array.isArray(material)) {
                material.forEach(FreeThree.disposeMaterial)
            } else {
                FreeThree.disposeMaterial(material)
            }
        }
    }

    static disposeMaterial(material) {
        material.dispose()
        Object.keys(material).forEach((key) => {
            const value = material[key]
            if (value?.isTexture) value.dispose()
        })
    }
}
